/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCheckout.java
 * Renato ferreira dos santos
 * Created on 25/03/2010, 09:55:08
 */

package forms;

import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import rad.Java.Common.DB.Search;
import rad.Java.MotelPlus.Class.Apt;
import rad.Java.MotelPlus.Class.AptType;
import rad.Java.MotelPlus.Class.Product;
import rad.Java.MotelPlus.Class.Sale;
import rad.Java.MotelPlus.Class.SaleDetail;
import rad.Java.MotelPlus.Class.StateApt;
import rad.Java.MotelPlus.Class.Stay;
import rad.Java.MotelPlus.Views.ProductSearch;

public class frmCheckout extends javax.swing.JDialog {

    // <editor-fold defaultstate="collapsed" desc="Declaration of variables">
        private Stay stay = new Stay();
        private Apt apt = new Apt();
        private Sale sale = new Sale();
        private SaleDetail saleDetail = new SaleDetail();
        private AptType apttype = new AptType();
        private StateApt state;
        DefaultTableModel dtmProdutos = new DefaultTableModel();
        private Double sumAll;
        Date date = new Date();
    // </editor-fold>
    /** Creates new form frmCheckout */
    public frmCheckout(Apt apt, Stay stay) throws SQLException {
        setModal(true);
        //this.sale = sale;
        this.apt = apt;
        this.stay = stay;
        initComponents();
        clear();
        fillFields();

        txtNameApt.setText(apt.getNumber());
        state = apt.getStatus();
        txtCodProduto.requestFocus();

       if (sale.getId().equals(""))
       {
           sale.setStay_id(stay.getId());
           if( !sale.save())
           {
               JOptionPane.showMessageDialog(this,
                                             "Erro ao gravar venda:\n"+sale.getErrorMessage(),
                                             "Erro", JOptionPane.ERROR_MESSAGE);
               return;
           }
       }
    }

    // <editor-fold defaultstate="collapsed" desc="Functions">
        private void clear(){
            txtCodProduto.setText("");
            txtDataEntrada.setText("");
            txtDiscriminacao.setText("");
            txtFuncionario.setText("Moreninha");
            txtHoraEntrada.setText("");
            txtNPessoas.setText("");
            txtPlaca.setText("");
            txtQuant.setText("1");
            txtTempoQuarto.setText("");
            txtTipoQuarto.setText("");
            txtValor.setText("0.00");
            txtVeiculo.setText("");
            txtDesconto.setText("0.00");
            txtAcrescimo.setText("0.00");
            txtTotalGeral.setText("0.00");
            txtConsumo.setText("0.00");
        }

        private void fillFields() throws SQLException
        {
            try
            {
                DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date aux = fmt.parse(stay.getInitDateTime());

                apttype.setId(apt.getTypeId());
                if (!apttype.get()) {
                    JOptionPane.showMessageDialog(null, apttype.getErrorMessage());
                    return;
                }
                txtVeiculo.setText(stay.getVehicle());
                txtPlaca.setText(stay.getPlaque());
                txtTipoQuarto.setText(apttype.getShortName());
                txtFuncionario.setText(null);
                txtDataEntrada.setText(new SimpleDateFormat("dd/MM/yyyy").format(aux));
                txtHoraEntrada.setText(new SimpleDateFormat("HH:mm:ss").format(aux));
                txtNPessoas.setText(stay.getPersonNumber());
                txtTempoQuarto.setText(new Time(date.getTime() - aux.getTime() + 10800000).toString());
                fillTblMain();
            } catch (ParseException ex) {
                Logger.getLogger(frmCheckout.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        private void setClass()
        {
            DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

           try
           {
               sale.setPrice(txtConsumo.getText());
               sale.setIncrement(txtAcrescimo.getText());
               sale.setDiscount(txtDesconto.getText());
               sale.setTotal(txtTotalGeral.getText());
           }
           catch(Exception e){}            
           
            if( !sale.update())
            {
                JOptionPane.showMessageDialog(this,
                                              "Erro ao tentar gravar venda!\n"+sale.getErrorMessage(),
                                              "Erro",
                                              JOptionPane.ERROR_MESSAGE);
                return;
            }

            stay.setEndDateTime(fmt.format(date));
            stay.setSalePrice(txtConsumo.getText());
            stay.setTotal(txtTotalGeral.getText());
            stay.setDiscount(txtDesconto.getText());
            stay.setIncrement(txtAcrescimo.getText());

            //TODO: Colocar o id do funcionário
            stay.setEmployeeIdCheckout("-1");


        }

        private void fillTblMain()
        {
            clearTblMain();
            String produtos[] = new String[5];
            sumAll = 0.0; //Variable of sum
            Float amount, price;
            try {
                Search search = new Search();
                search.prepare();
                search.setFields("*");
                search.setTable("stay");
                search.setTable("sale", "sale_stay_id = stay_id");
                search.setTable("sale_detail", "sale_detail_sale_id = sale_id");
                search.setTable("product", "sale_detail_product_id = product_id");
                search.setConditionalWhere("stay_id ="+stay.getId());
                search.setAdditional("ORDER BY sale_detail_id");
                if (!search.execute()) {
                    JOptionPane.showMessageDialog(null, search.getMessageError());
                    return;
                }
                while (search.next()) {
                    produtos[0] = search.getValue("product_code").toString();
                    produtos[1] = search.getValue("sale_detail_amount").toString();
                    produtos[2] = search.getValue("product_name").toString();
                    produtos[3] = search.getValue("product_price").toString();
                    amount = Float.valueOf(produtos[1]);
                    price  = Float.valueOf(produtos[3]);
                    float total = amount * price;
                    produtos[4] = String.valueOf(total);
                    dtmProdutos.addRow(produtos);
                    sumAll += total;

                    sale.setId(search.getValue("sale_id").toString());
                }
                dtmProdutos.removeRow(0);
                tblMain.setModel(dtmProdutos);
                txtConsumo.setText(String.valueOf(sumAll));
                updateValues();
            } catch (SQLException ex) {
                Logger.getLogger(frmCheckout.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        private void updateValues()
        {
            float subTotal, discount, increment, total;
            
            try
            {
                subTotal = Float.parseFloat(txtConsumo.getText());
                discount = Float.parseFloat(txtDesconto.getText());
                increment = Float.parseFloat(txtAcrescimo.getText());

                total = subTotal - discount + increment;
                txtTotalGeral.setText(String.valueOf(total));
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(this,
                                              "Valores incorretos:\n"+e.getMessage(),
                                              "Erro",
                                              JOptionPane.ERROR_MESSAGE);
            }
        }

        private void saleProduct()
        {
           Product product;
           try
           {
               Double.parseDouble(txtQuant.getText());
           }
           catch(Exception e)
           {
               JOptionPane.showMessageDialog(this, "Quantidade inválida!", "Erro", JOptionPane.ERROR_MESSAGE);
               return;
           }
           product = new ProductSearch(null, true).execute(txtCodProduto.getText());

           if(product == null)
               return;
           
           saleDetail = new SaleDetail();
           //FIXME: modificar
           saleDetail.setEmployee_id("0");
           saleDetail.setProduct_id(product.getId());
           saleDetail.setPrice(product.getPrice());
           saleDetail.setAmount(txtQuant.getText());
           saleDetail.setSale_id(sale.getId());

           if (!saleDetail.save())
           {
                   JOptionPane.showMessageDialog(this,
                                                 "Erro ao gravar venda:\n"+saleDetail.getErrorMessage(),
                                                 "Erro", JOptionPane.ERROR_MESSAGE);
                   return;
           }
           fillTblMain();
        }

        private void clearTblMain()
        {
            String[] colunas = new String[]{"Código","Quantidade","Discriminação","Valor","Total"};
            String[][] dados = new String[][]{{}};
            dtmProdutos = new DefaultTableModel(dados, colunas);
            tblMain.setModel(dtmProdutos);
        }

        public StateApt showForm()
        {
            //setLocation(MouseInfo.getPointerInfo().getLocation());
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    txtCodProduto.requestFocusInWindow();
                }
            });
            setVisible(true);

            return state;
        }
    // </editor-fold>
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtVeiculo = new javax.swing.JTextField();
        txtFuncionario = new javax.swing.JTextField();
        txtPlaca = new javax.swing.JTextField();
        txtDataEntrada = new javax.swing.JFormattedTextField();
        txtTipoQuarto = new javax.swing.JTextField();
        txtHoraEntrada = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtTempoQuarto = new javax.swing.JTextField();
        txtNPessoas = new javax.swing.JTextField();
        txtNameApt = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        txtDiscriminacao = new javax.swing.JTextField();
        txtCodProduto = new javax.swing.JTextField();
        txtQuant = new javax.swing.JTextField();
        txtValor = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblMain = new javax.swing.JTable();
        btnFinalizar = new javax.swing.JButton();
        btnOK = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        txtConsumo = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtAcrescimo = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtDesconto = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtTotalGeral = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setLocationByPlatform(true);
        setName("Form"); // NOI18N
        setResizable(false);
        setUndecorated(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel2.setName("jPanel2"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(motelplus.MotelPlusApp.class).getContext().getResourceMap(frmCheckout.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        txtVeiculo.setText(resourceMap.getString("txtVeiculo.text")); // NOI18N
        txtVeiculo.setName("txtVeiculo"); // NOI18N

        txtFuncionario.setText(resourceMap.getString("txtFuncionario.text")); // NOI18N
        txtFuncionario.setName("txtFuncionario"); // NOI18N

        txtPlaca.setText(resourceMap.getString("txtPlaca.text")); // NOI18N
        txtPlaca.setName("txtPlaca"); // NOI18N

        txtDataEntrada.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        txtDataEntrada.setText(resourceMap.getString("txtDataEntrada.text")); // NOI18N
        txtDataEntrada.setName("txtDataEntrada"); // NOI18N

        txtTipoQuarto.setText(resourceMap.getString("txtTipoQuarto.text")); // NOI18N
        txtTipoQuarto.setName("txtTipoQuarto"); // NOI18N

        txtHoraEntrada.setText(resourceMap.getString("txtHoraEntrada.text")); // NOI18N
        txtHoraEntrada.setName("txtHoraEntrada"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        txtTempoQuarto.setText(resourceMap.getString("txtTempoQuarto.text")); // NOI18N
        txtTempoQuarto.setName("txtTempoQuarto"); // NOI18N

        txtNPessoas.setText(resourceMap.getString("txtNPessoas.text")); // NOI18N
        txtNPessoas.setName("txtNPessoas"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5))
                        .addGap(161, 161, 161))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtVeiculo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtDataEntrada, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                        .addComponent(txtPlaca, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel2)
                    .addComponent(jLabel6))
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(txtTipoQuarto, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtHoraEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING))
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtTempoQuarto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel8)
                            .addContainerGap())
                        .addComponent(txtNPessoas, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTempoQuarto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTipoQuarto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNPessoas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtHoraEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtVeiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtDataEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        txtNameApt.setFont(resourceMap.getFont("txtNameApt.font")); // NOI18N
        txtNameApt.setText(resourceMap.getString("txtNameApt.text")); // NOI18N
        txtNameApt.setName("txtNameApt"); // NOI18N

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel4.setName("jPanel4"); // NOI18N

        jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
        jLabel14.setName("jLabel14"); // NOI18N

        jLabel15.setText(resourceMap.getString("jLabel15.text")); // NOI18N
        jLabel15.setName("jLabel15"); // NOI18N

        jLabel16.setText(resourceMap.getString("jLabel16.text")); // NOI18N
        jLabel16.setName("jLabel16"); // NOI18N

        txtDiscriminacao.setText(resourceMap.getString("txtDiscriminacao.text")); // NOI18N
        txtDiscriminacao.setName("txtDiscriminacao"); // NOI18N

        txtCodProduto.setText(resourceMap.getString("txtCodProduto.text")); // NOI18N
        txtCodProduto.setName("txtCodProduto"); // NOI18N
        txtCodProduto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCodProdutoKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCodProdutoKeyTyped(evt);
            }
        });

        txtQuant.setEditable(false);
        txtQuant.setText(resourceMap.getString("txtQuant.text")); // NOI18N
        txtQuant.setName("txtQuant"); // NOI18N

        txtValor.setText(resourceMap.getString("txtValor.text")); // NOI18N
        txtValor.setName("txtValor"); // NOI18N

        jLabel17.setText(resourceMap.getString("jLabel17.text")); // NOI18N
        jLabel17.setName("jLabel17"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        tblMain.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblMain.setName("tblMain"); // NOI18N
        jScrollPane3.setViewportView(tblMain);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel17)
                    .addComponent(txtQuant, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(txtCodProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel15)
                    .addComponent(txtDiscriminacao, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addContainerGap(89, Short.MAX_VALUE))
                    .addComponent(txtValor, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)))
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(jLabel14)
                    .addComponent(jLabel16)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtQuant, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCodProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDiscriminacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
        );

        btnFinalizar.setText(resourceMap.getString("btnFinalizar.text")); // NOI18N
        btnFinalizar.setName("btnFinalizar"); // NOI18N
        btnFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFinalizarActionPerformed(evt);
            }
        });

        btnOK.setText(resourceMap.getString("btnOK.text")); // NOI18N
        btnOK.setName("btnOK"); // NOI18N
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel3.setName("jPanel3"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        txtConsumo.setEditable(false);
        txtConsumo.setText(resourceMap.getString("txtConsumo.text")); // NOI18N
        txtConsumo.setName("txtConsumo"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        txtAcrescimo.setText(resourceMap.getString("txtAcrescimo.text")); // NOI18N
        txtAcrescimo.setName("txtAcrescimo"); // NOI18N
        txtAcrescimo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtAcrescimoFocusLost(evt);
            }
        });

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        txtDesconto.setText(resourceMap.getString("txtDesconto.text")); // NOI18N
        txtDesconto.setName("txtDesconto"); // NOI18N
        txtDesconto.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDescontoFocusLost(evt);
            }
        });

        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N

        txtTotalGeral.setEditable(false);
        txtTotalGeral.setText(resourceMap.getString("txtTotalGeral.text")); // NOI18N
        txtTotalGeral.setName("txtTotalGeral"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(txtConsumo, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
            .addComponent(txtAcrescimo, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel9)
                .addContainerGap())
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel11)
                .addContainerGap())
            .addComponent(txtDesconto, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel13)
                .addContainerGap())
            .addComponent(txtTotalGeral, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel10)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtConsumo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtAcrescimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel11)
                .addGap(3, 3, 3)
                .addComponent(txtDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtTotalGeral, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(88, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(btnFinalizar, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(305, 305, 305)
                        .addComponent(txtNameApt)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnFinalizar, btnOK});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtNameApt)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnOK, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                    .addComponent(btnFinalizar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-746)/2, (screenSize.height-444)/2, 746, 444);
    }// </editor-fold>//GEN-END:initComponents

    private void txtDescontoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDescontoFocusLost
        updateValues();
    }//GEN-LAST:event_txtDescontoFocusLost

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        dispose();
    }//GEN-LAST:event_btnOKActionPerformed

    private void txtCodProdutoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodProdutoKeyPressed

    }//GEN-LAST:event_txtCodProdutoKeyPressed


    private void txtCodProdutoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodProdutoKeyTyped
        double aux;

        if (evt.getKeyChar() == '*')
        {
            aux = Double.parseDouble(txtCodProduto.getText());

            txtQuant.setText(String.valueOf(aux));
            txtCodProduto.setText("");
            evt.consume();
        }
        else if (evt.getKeyChar() == KeyEvent.VK_ENTER)
        {
            saleProduct();
            txtCodProduto.setText("");
            txtQuant.setText("1");

            txtCodProduto.requestFocus();
        }
    }//GEN-LAST:event_txtCodProdutoKeyTyped

    private void btnFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFinalizarActionPerformed
        setClass();
        dlgFinalizeAccount finalize = new dlgFinalizeAccount(null, true);
        finalize.setApt(apt);
        finalize.setStay(stay);

        if (finalize.executeFinalize())
        {
            state = apt.getStatus();
            this.dispose();
        }
    }//GEN-LAST:event_btnFinalizarActionPerformed

    private void txtAcrescimoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtAcrescimoFocusLost
        updateValues();
    }//GEN-LAST:event_txtAcrescimoFocusLost

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFinalizar;
    private javax.swing.JButton btnOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tblMain;
    private javax.swing.JTextField txtAcrescimo;
    private javax.swing.JTextField txtCodProduto;
    private javax.swing.JTextField txtConsumo;
    private javax.swing.JFormattedTextField txtDataEntrada;
    private javax.swing.JTextField txtDesconto;
    private javax.swing.JTextField txtDiscriminacao;
    private javax.swing.JTextField txtFuncionario;
    private javax.swing.JTextField txtHoraEntrada;
    private javax.swing.JTextField txtNPessoas;
    private javax.swing.JLabel txtNameApt;
    private javax.swing.JTextField txtPlaca;
    private javax.swing.JTextField txtQuant;
    private javax.swing.JTextField txtTempoQuarto;
    private javax.swing.JTextField txtTipoQuarto;
    private javax.swing.JTextField txtTotalGeral;
    private javax.swing.JTextField txtValor;
    private javax.swing.JTextField txtVeiculo;
    // End of variables declaration//GEN-END:variables

}
